<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="titleText" :visible="showDialog" @close="btnCancle">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addForm" label-width="120px" :rules="rules" :model="formData">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width:80%"
          placeholder="请选择"
          @focus="getSimpleList"
          @blur="dealBlur"
        >
          <el-option
            v-for="item in employList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancle">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartmentDetail, editDepartments } from '../../api/departments.js'
import { getSimpleEmployeeList } from '../../api/employee.js'
export default {
  props: {
    // 是否显示表单
    showDialog: {
      type: Boolean,
      required: true
    },
    nodeValue: {
      type: Object,
      required: true
    }
  },
  data() {
    // 检测部门名称是否重复
    const checkedNameRepeat = async(rule, value, callback) => {
      // 定义一个布尔值
      let isRepeat = false
      // 获取部门列表
      const { depts } = await getDepartments()
      // 判断是编辑还是新增
      if (this.formData.id) {
        // 编辑
        isRepeat = depts
          .filter(v => v.pid === this.nodeValue.pid)
          .some(v => v.name === value && v.id !== this.formData.id)
      } else {
        // 新增
        isRepeat = depts
          .filter(v => v.pid === this.nodeValue.id)
          .some(v => v.name === value)
      }
      // 处理
      if (isRepeat) {
        callback(new Error('同级部门下已有该部门名称'))
      } else {
        callback()
      }
    }
    // 检测部门编码是否重复
    const checkedCodeRepeat = async(rule, value, callback) => {
      // 定义一个布尔值
      let isRepeat = false
      // 获取部门列表
      const { depts } = await getDepartments()
      // 判断是编辑还是新增
      if (this.formData.id) {
        // 编辑
        isRepeat = depts
          .some(v => v.code === value && v.id !== this.formData.id)
      } else {
        // 新增
        isRepeat = depts
          .some(v => v.code === value)
      }
      // 处理
      if (isRepeat) {
        callback(new Error('公司部门编码要求唯一'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      // 校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkedNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkedCodeRepeat, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-300个字符' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ]
      },
      // 部门负责人列表
      employList: []
    }
  },
  // 标题
  computed: {
    titleText() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    // 获取部门负责人简单列表
    async getSimpleList() {
      const res = await getSimpleEmployeeList()
      this.employList = res
    },
    // 手动校验部门负责人
    dealBlur() {
      setTimeout(() => {
        this.$refs.addForm.validateField('manager')
      }, 200)
    },
    // 确认按钮
    async btnOK() {
      // 校验表单
      await this.$refs.addForm.validate()
      if (this.formData.id) {
        // 发送编辑请求
        await editDepartments(this.formData)
      } else {
        // 发送新增请求
        await addDepartments({ ...this.formData, pid: this.nodeValue.id })
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 更新页面
      this.$emit('updateList')
    },
    // 取消按钮
    btnCancle() {
      // 重置内容
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
      // 清除校验结果
      this.$refs.addForm.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    async dealEdit(id) {
      this.formData = await getDepartmentDetail(id)
    }
  }
}
</script>

<style>

</style>
